
import os
import sys

import wx
import wx.html

try:
    import win32api
    import win32con
    import win32help
    hasWin32 = True
except ImportError:
    hasWin32 = False

import ab_ids as ai

from wx.lib.wordwrap import wordwrap

try:
    from __version__ import author
except ImportError:
    author = 'cadappl'

try:
    from __version__ import version
except ImportError:
    version = '(unknown-version)'


def getApplicationName():
    return "Analyzer of Files"


def getApplitionDescription():
    return """\
AnaFile is a program to support file content recognization.

To reach the purpose, it provides the core to support multiple plug-ins. And \
five types of plug-ins are for the specified file format compatibilities with \
smart enhancement."""


def getApplitionCopyright():
    return "(C) 2008-2014"


def getApplicationVersionString():
    return version


def showAboutDialog(win):
    pyver = sys.version.split()[0]
    wxver = wx.VERSION_STRING

    info = wx.AboutDialogInfo()

    info.Name = getApplicationName()
    info.Version = getApplicationVersionString()
    info.Copyright = "\nPython: %s, wxPython: %s" % (pyver, wxver)
    info.Description = wordwrap(getApplitionDescription(),
                                400, wx.ClientDC(win))

    info.License = wordwrap("""\
This program is free software; you can redistribute it and/or modify it under \
the terms of the GNU General Public License as published by the Free Software \
Foundation; either version 2 of the License, or (at your option) any later \
version.

A copy of the GNU General Public License can be found at http://www.gnu.org/\
""", 350, wx.ClientDC(win))

    info.Developers = author.split(',')
    wx.AboutBox(info)


wx.FileSystem.AddHandler(wx.ZipFSHandler())


class HelpContent:
    def __init__(self, app):
        self.app = app
        self.helpFile = None
        self.helpCtrl = None
        self.helpHandler = None

        home_dir = self.app.readKey(ai.CfgParaCwd)
        supportChm = self.app.readKey(ai.CfgParaPlatform).hasChmfileSupport()
        if hasWin32 and supportChm:
            chmFiles = (
                os.path.join(home_dir, 'aof.chm'),
                os.path.join(home_dir, 'doc', 'aof.chm'),
                os.path.join(home_dir, 'build', 'chm', 'aof.chm'))

            for hlpFile in chmFiles:
                if os.path.exists(hlpFile):
                    self.helpFile = hlpFile
                    break

            if self.helpFile is not None:
                try:
                    frame = 0
                    _, self.helpHandler = win32help.HtmlHelp(
                        frame, None, win32help.HH_INITIALIZE)
                except:
                    self.helpFile = None

        if self.helpFile is None:
            self.helpCtrl = wx.html.HtmlHelpController()

            helpFiles = (
                os.path.join(home_dir, 'aof.htb'),
                os.path.join(home_dir, 'doc', 'aof.htb'),
                os.path.join(home_dir, 'build', 'htb', 'aof.htb'),
                os.path.join(home_dir, 'build', 'htmlhelp', 'aof.hhp'))

            # for HTB and HHP file, we just need one
            for hlpFile in helpFiles:
                if os.path.exists(hlpFile) and self.helpCtrl.AddBook(hlpFile):
                    self.helpFile = hlpFile
                    break

    def release(self):
        if self.helpHandler is not None:
            frame = 0
            win32help.HtmlHelp(
                frame, None, win32help.HH_UNINITIALIZE,
                self.helpHandler)

    def isEnable(self):
        return self.helpFile is not None and os.path.exists(self.helpFile)

    def showHelpContents(self):
        if self.helpCtrl is not None:
            self.helpCtrl.DisplayContents()
        else:
            win32api.ShellExecute(
                0, 'open', self.helpFile, None, '', win32con.SW_SHOW)

    def showHelpIndex(self):
        if self.helpCtrl is not None:
            self.helpCtrl.DisplayIndex()
        else:
            frame = 0
            win32help.HtmlHelp(
                frame, self.helpFile, win32help.HH_DISPLAY_INDEX, '')
